<script setup lang="js">
    import { ref, computed } from 'vue';
    import { ElMessage } from "element-plus";
    import axios from 'axios';
    import { useRouter } from 'vue-router';
    import request from '../../request/axios'
    const router = useRouter();

    // 添加计算未读数的方法
    const messageList = ref([])
    const unreadCount = computed(() => {
        return messageList.value.reduce((count, message) => {
            return message.notice_status == 1 ? count + 1 : count
        }, 0)
    })
    // 消息列表
    const getMessageList = async () => {
        try {
            const res = await request.getV2({ url: 'v1/api-msg-list?page=1&pageNum=99&status=1' });
            messageList.value = res.data.list || [];
        } catch (error) {
            console.log(error);
        }
    }
    getMessageList();


    const userInfo = ref({})
    const getUserMessage = async () => {
        try {
            const res = await request.getV2({ url: '/v1/api-user-Info' });
            localStorage.setItem("userInfo", JSON.stringify(res.data))
            userInfo.value = res.data
        } catch (error) {
            console.log(error);
        }
    }
    getUserMessage()

    const clickDelLogin = async () => {
        try {
            const res = await request.postV2({ url: '/v1/api-user-logout' });
            ElMessage.success('退出成功！');
            localStorage.removeItem("token");
            router.push('/')
        } catch (error) {
            console.log(error);
        }
    }
</script>

<template>
    <div class="content-settings">
        <div class="head-content" style="margin-top: 40px;">
            <ArrowLeft class="head-icon" @click="router.back()" />
        </div>
        <div class="settings-box">
            <div class="settings-title">账号设置</div>
            <div class="card-form">
                <div class="form-box" style="cursor: pointer;" @click="() => router.push('/userInfo')">
                    昵称
                    <div class="right-tips">{{ userInfo.nickname }}
                        <ArrowRight class="right-icon" />
                    </div>
                </div>
                <div class="form-box" style="cursor: pointer;" @click="() => router.push('/userInfo')">
                    手机号
                    <div class="right-tips">{{ userInfo.phone }}
                        <ArrowRight class="right-icon" />
                    </div>
                </div>
                <div class="form-box" style="cursor: pointer;" @click="() => router.push('/userInfo')">
                    当前身份
                    <div class="right-tips">
                        {{userInfo.is_company_member == 1?'管理员':userInfo.is_member == 1?'会员':'普通用户'}}
                        <ArrowRight class="right-icon" />
                    </div>
                </div>
            </div>
            <div class="settings-title">数据管理</div>
            <div class="card-form">
                <div class="form-box">
                    体验优化计划
                    <div class="right-tips">
                        <el-switch v-model="value1" />
                    </div>
                </div>
                <!-- <div class="form-box">
                    模型配置
                    <div class="right-tips">
                        <ArrowRight class="right-icon" />
                    </div>
                </div> -->

            </div>
            <div class="settings-title">消息中心</div>
            <div class="card-form">
                <div class="form-box" style="position: relative;" @click="() => router.push('/message')">
                    消息列表
                    <span class="message-num" v-if="unreadCount > 0">{{unreadCount}}</span>
                    <div class="right-tips">
                        <ArrowRight class="right-icon" />
                    </div>
                </div>
            </div>

            <div class="settings-title">其他</div>
            <div class="card-form">
                <div class="form-box">
                    使用帮助
                    <div class="right-tips">
                        <ArrowRight class="right-icon" />
                    </div>
                </div>
                <div class="form-box">
                    意见反馈
                    <div class="right-tips">
                        <ArrowRight class="right-icon" />
                    </div>
                </div>
            </div>

            <el-button type="info" @click="clickDelLogin" class="del-login">退出登录</el-button>
        </div>
    </div>
</template>

<style scoped lang="scss">
    .message-num {
        position: absolute;
        width: 18px !important;
        height: 18px;
        border-radius: 50%;
        background: red;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 12px !important;
        color: #fff !important;
        top: 5px;
        left: 70px;
    }

    .content-settings {
        width: 100%;
    }

    .settings-box {
        width: 850px;
        height: 85vh;
        text-align: center;
        overflow-y: auto;
        margin: 0 auto;

        .settings-title {
            font-size: 18px;
            font-weight: bold;
            margin: 18px 0 18px;
            color: #000000;
            text-align: left;
        }

        .card-form {
            background-color: #fff;
            border-radius: 10px;
            box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.05);

            .form-box {
                display: flex;
                align-items: center;
                justify-content: space-between;
                font-size: 18px;
                padding: 18px 18px 18px 0;
                margin-left: 18px;
                cursor: pointer;
                border-bottom: 1px solid rgba(216, 216, 216, 0.5);

                &:last-child {
                    border-bottom: none;
                }

                .right-tips {
                    color: #999999;
                    display: flex;
                    align-items: center;
                    justify-content: center;

                    .right-icon {
                        color: #66969799;
                        width: 20px;
                        height: 20px;
                        margin-left: 6px;
                    }
                }
            }
        }
    }

    .del-login {
        width: 800px;
        height: 50px;
        margin-top: 30px;
        border-radius: 12px;
    }
</style>